import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* Router Modules */
// 模块化，首先引入在modules目录下需要用的router

/**
 * constantRoutes —— 静态路由
 * 不需要动态判断权限的路由，如登录页、404等通用页面
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  // 登录页面
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  // auth-redict
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  // 404
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  // 401
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },

  // 仪表盘（首页）
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  },

  // 个人
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: '个人信息', icon: 'user', noCache: true }
      }
    ]
  }
]

/**
 * asyncRoutes —— 动态路由
 * 需要动态判断权限并通过addRoutes动态添加的页面
 */
export const asyncRoutes = [
  // 广告管理
  {
    path: '/manage-ad',
    component: Layout,
    redirect: '/manage-ad/list',
    name: '广告管理',
    meta: {
      title: '广告管理',
      icon: 'ad',
      roles:['admin','editor']
    },
    children: [
      {
        path: 'create',
        component: () => import('@/views/manage-ad/create'),
        name: 'CreateAd',
        meta: {
          title: '广告发布',
          icon: 'edit'
        }
      },
      {
        path: 'edit/:id(\\d+)',
        component: () => import('@/views/manage-ad/list'),
        name: 'EditAd',
        meta: {
          title: '广告修改',
          noCache: true,
          activeMenu: '/manage-ad/list'
        },
        hidden: true
      },
      {
        path: 'list',
        component: () => import('@/views/manage-ad/list'),
        name: 'AdList',
        meta: { title: '广告列表', icon: 'list' }
      }
    ]
  },
  // 任务管理
  {
    path: '/manage-task',
    component: Layout,
    redirect: '/manage-task/list',
    name: '任务管理',
    meta: {
      title: '任务管理',
      icon: 'task',
      roles:['admin','editor']
    },
    children: [
      {
        path: 'create',
        component: () => import('@/views/manage-task/create'),
        name: 'CreateTask',
        meta: {
          title: '任务发布',
          icon: 'edit'
        }
      },
      {
        path: 'edit/:id(\\d+)',
        component: () => import('@/views/manage-task/list'),
        name: 'EditTask',
        meta: {
          title: '任务修改',
          noCache: true,
          activeMenu: '/manage-task/list'
        },
        hidden: true
      },
      {
        path: 'list',
        component: () => import('@/views/manage-task/list'),
        name: 'TaskList',
        meta: { title: '任务列表', icon: 'list' }
      }
    ]
  },
  // 学生管理
  {
    path: '/manage-student',
    component: Layout,
    redirect: '/manage-student/list',
    name: '学生管理',
    meta: {
      title: '学生管理',
      icon: 'student',
      roles:['admin','editor']
    },
    children: [
      {
        path: 'upload',
        component: () => import('@/views/manage-student/upload'),
        name: 'UploadStudent',
        meta: {
          title: '信息上传',
          icon: 'edit'
        }
      },
      {
        path: 'edit/:id(\\d+)',
        component: () => import('@/views/manage-student/list'),
        name: 'EditStudent',
        meta: {
          title: '信息修改',
          noCache: true,
          activeMenu: '/manage-student/list'
        },
        hidden: true
      },
      {
        path: 'list',
        component: () => import('@/views/manage-student/list'),
        name: 'StudentList',
        meta: { title: '学生列表', icon: 'list' }
      }
    ]
  },
  // 礼品管理
  {
    path: '/manage-gift',
    component: Layout,
    redirect: '/manage-gift/list',
    name: '礼品管理',
    meta: {
      title: '礼品管理',
      icon: 'gift',
      roles:['admin','editor']
    },
    children: [
      {
        path: 'create',
        component: () => import('@/views/manage-gift/create'),
        name: 'CreateGift',
        meta: {
          title: '礼品发布',
          icon: 'edit'
        }
      },
      {
        path: 'edit/:id(\\d+)',
        component: () => import('@/views/manage-gift/list'),
        name: 'EditGift',
        meta: {
          title: '礼品修改',
          noCache: true,
          activeMenu: '/manage-gift/list'
        },
        hidden: true
      },
      {
        path: 'list',
        component: () => import('@/views/manage-gift/list'),
        name: 'GiftList',
        meta: { title: '礼品列表', icon: 'list' }
      }
    ]
  },
  //权限管理
  {
    path: '/permission',
    component: Layout,
    redirect: '/permission/role',
    children: [
      {
        path: 'role',
        component: () => import('@/views/manage-permission/role'),
        name: 'Permission',
        meta: { title: '权限管理', icon: 'lock', roles:['admin']}
      }
    ]
  },

 


  // 404 page必须放在最后
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
